package indi.jp.project.business.jingying.service.impl;

import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.business.jingying.entity.JyProject;
import indi.jp.project.business.jingying.entity.JyProjectTrace;
import indi.jp.project.business.jingying.service.JyProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class JyProjectServiceImpl implements JyProjectService {

    private BaseDao baseDao;

    public JyProjectServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Map<String, Object> queryGrid(JyProject entity) throws SQLException {
        List<JyProject> list = baseDao.queryList("jyProjectMapper.queryList", entity);
        int count = baseDao.queryCount("jyProjectMapper.queryCount", entity);
        Map<String, Object> map = new HashMap<>();
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public JsonResult save(JyProject entity) throws SQLException {
        if("".equals(entity.getId())){
            String id = UUID.randomUUID().toString();
            entity.setId(id);
            int effectRow = baseDao.insert("jyProjectMapper.save", entity);
            if(effectRow > 0){
                return JsonResult.success(id);
            } else {
                return JsonResult.failure("保存失败");
            }
        } else {
            int effectRow =  baseDao.update("jyProjectMapper.edit", entity);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("保存失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteById(String id) throws SQLException {
        baseDao.deleteById("jyProjectMapper.deleteProjectTraceByBillId", id);
        baseDao.deleteById("jyProjectMapper.deleteById", id);
        return 1;
    }

    @Override
    public JyProject queryDetailById(String id) throws SQLException {
        return (JyProject) baseDao.findById("jyProjectMapper.queryDetailById", id);
    }

    @Override
    public Map<String, Object> queryProjectTraceGrid(JyProjectTrace entity) throws SQLException {
        List<JyProject> list = baseDao.queryList("jyProjectMapper.queryProjectTraceList", entity);
        int count = baseDao.queryCount("jyProjectMapper.queryProjectTraceCount", entity);
        Map<String, Object> map = new HashMap<>();
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public int saveProjectTrace(JyProjectTrace entity) throws SQLException {
        if("".equals(entity.getId())){
            return baseDao.insert("jyProjectMapper.saveProjectTrace", entity);
        } else {
            return baseDao.update("jyProjectMapper.editProjectTrace", entity);
        }
    }

    @Override
    public int deleteProjectTraceById(String id) throws SQLException {
        return baseDao.deleteById("jyProjectMapper.deleteProjectTraceById", id);
    }
}
